package com.ruoyi.utils;

import com.ruoyi.config.WechatAccountConfig;
import com.ruoyi.utils.domain.*;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
@Component
public class WxTemplentMsg {
    @Autowired
    private WxMpService wxService;
    @Autowired
    private WechatAccountConfig wxMpProperties;
    /**
     * 系统故障通知
     * @return
     */
    public  String sysTemGuZhangTemplent(SysError sysError) {
        String OrderMsgTemplateId = "ZmdCFHDquN6dypSsQTySe2l1O_NStP-lugDN5M4xyPA";

        // 此处的 key/value 需和模板消息对应
        List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("thing2", sysError.getName(), "#000000"),
                new WxMpTemplateData("thing3", sysError.getLocaltion()),
                new WxMpTemplateData("thing6", sysError.getErrorName()),
                new WxMpTemplateData("time4", sysError.getErrorTime())
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(sysError.getOpenId())
                .templateId(OrderMsgTemplateId)
                .data(wxMpTemplateDataList)
//                .url(detailUrl)
                .miniProgram(new WxMpTemplateMessage.MiniProgram(wxMpProperties.getAppId(),"/pages/notice/detail?noticeId="+sysError.getId(),false))
                .build();

        try {
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            return "-1";
        }
        return sysError.getOpenId();
    }

    /**
     * 系统故障解除通知
     * @return
     */
    public String sysTemErrorHuiFuTemplent(SysHuiFu sysHuiFu) {
        String OrderMsgTemplateId = "S7Tco91-lGwdTidEAJwr1WFsjyzp5Vop8uHXywcIgaY";

        // 此处的 key/value 需和模板消息对应
        List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("thing2", sysHuiFu.getName(), "#000000"),
                new WxMpTemplateData("thing3", sysHuiFu.getLocaltion()),
                new WxMpTemplateData("thing6", sysHuiFu.getErrorName()),
                new WxMpTemplateData("time4", sysHuiFu.getHuiFuTime())
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(sysHuiFu.getOpenId())
                .templateId(OrderMsgTemplateId)
                .data(wxMpTemplateDataList)
//                .url(detailUrl)
                .miniProgram(new WxMpTemplateMessage.MiniProgram(wxMpProperties.getAppId(),"/pages/notice/detail?noticeId="+sysHuiFu.getId(),false))
                .build();

        try {
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            return "-1";
        }
        return sysHuiFu.getOpenId();
    }



    /**
     * 退租通知
     * @return
     */
    public String tuizu(TuiZu tuiZu) {
        String OrderMsgTemplateId = "Usbmurgyt9KfVtOrFuAREo13veHeJIIN5yNafDaKA00";

        // 此处的 key/value 需和模板消息对应
        List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("thing1", tuiZu.getFangWu(), "#000000"),
                new WxMpTemplateData("thing2", tuiZu.getPeople()),
                new WxMpTemplateData("time3", tuiZu.getTuiZuTime())
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(tuiZu.getOpenId())
                .templateId(OrderMsgTemplateId)
                .data(wxMpTemplateDataList)
//                .url(detailUrl)
                .miniProgram(new WxMpTemplateMessage.MiniProgram(wxMpProperties.getAppId(),"pages/records/list",false))
                .build();

        try {
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            return "-1";
        }
        return tuiZu.getOpenId();
    }


    /**
     * 续租通知
     * @return
     */
    public String xuZu(XuZu xuZu) {
        String OrderMsgTemplateId = "52I_9_PX9DakZzKo4iNeTyZzOHwAJHmIWkKW32jdC04";

        // 此处的 key/value 需和模板消息对应
        List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("thing1", xuZu.getFangWu(), "#000000"),
                new WxMpTemplateData("thing9", xuZu.getPeople()),
                new WxMpTemplateData("time5", xuZu.getXuZuTime())
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(xuZu.getOpenId())
                .templateId(OrderMsgTemplateId)
                .data(wxMpTemplateDataList)
//                .url(detailUrl)
                .miniProgram(new WxMpTemplateMessage.MiniProgram(wxMpProperties.getAppId(),"pages/records/list",false))
                .build();

        try {
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            return "-1";
        }
        return xuZu.getOpenId();
    }


    /**
     * 入住通知
     * @return
     */
    public String ruZu(RuZhu ruZhu) {
        String OrderMsgTemplateId = "CORnUlHfKJYHI4SoTgQeoHQWTxQaMDsX8POswZ3GRak";

        // 此处的 key/value 需和模板消息对应
        List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("thing4", ruZhu.getFangWu(), "#000000"),
                new WxMpTemplateData("time9", ruZhu.getRuZhuTime()),
                new WxMpTemplateData("thing2", ruZhu.getPeople())
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(ruZhu.getOpenId())
                .templateId(OrderMsgTemplateId)
                .data(wxMpTemplateDataList)
//                .url(detailUrl)
                .miniProgram(new WxMpTemplateMessage.MiniProgram(wxMpProperties.getAppId(),"pages/records/list",false))
                .build();

        try {
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            return "-1";
        }
        return ruZhu.getOpenId();
    } /**
     * 入住审核
     * @return
     */
    public String RuZhuShenHe(RuZhuShenHe ruZhuShenHe) {
        String OrderMsgTemplateId = "RNdqffo-ymZVN-OZksRPBxqd4cGME_AxBZsxhuaeVAo";

        // 此处的 key/value 需和模板消息对应
        List<WxMpTemplateData> wxMpTemplateDataList = Arrays.asList(
                new WxMpTemplateData("thing1", ruZhuShenHe.getPeople(), "#000000"),
                new WxMpTemplateData("phone_number2", ruZhuShenHe.getPhone()),
                new WxMpTemplateData("thing3", ruZhuShenHe.getFangWu())
        );

        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(ruZhuShenHe.getOpenId())
                .templateId(OrderMsgTemplateId)
                .data(wxMpTemplateDataList)
//                .url(detailUrl)
                .miniProgram(new WxMpTemplateMessage.MiniProgram(wxMpProperties.getAppId(),"pages/mine/uploadinfo/index",false))
                .build();

        try {
            wxService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            return "-1";
        }
        return ruZhuShenHe.getOpenId();
    }

}
